import { NextRequest } from "next/server";

function iteratorToStream(iterator: any) {
    return new ReadableStream({
        async pull(controller) {
            const { value, done } = await iterator.next();

            if (done) {
                controller.close();
            } else {
                controller.enqueue(value);
            }
        },
    });
}

function sleep(time: number) {
    return new Promise((resolve) => {
        setTimeout(resolve, time);
    });
}

const encoder = new TextEncoder();

async function* makeIterator() {
    yield encoder.encode("<p>One</p>");
    await sleep(2000);
    yield encoder.encode("<p>Two</p>");
    await sleep(2000);
    yield encoder.encode("<p>Three</p>");
}
export const GET = (req: NextRequest) => {
    const iterator = makeIterator();
    const stream = iteratorToStream(iterator);

    return new Response(stream);
};
